/*
 * =====================================================================================
 *
 *       Filename:  calculate.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  09/10/18 00:10:54
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Andy
 *        Company:  
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <math.h>
#include "csv.h"
#include "calculate.h"

double getAverage(double **data, int row, int col)
{
    double sum = 0.0;
    for (int j = 0; j < col; j++) {
        sum += data[row][j];    
    }
    double ave = sum / col;
    return ave;
}

double getMax(double **data, int row, int col)
{
    double max = data[row][0];
    for (int j = 0; j < col; j++) {
        if (fabs(max) < fabs(data[row][j])) {
            max = data[row][j]; 
        }    
    }
    return max;

}

AngleItem getValue(const char *filename)
{
    char line[1024];
    double **data;
    int row, col;
    row = get_row(filename);
    col = get_col(filename);
    data = (double **)malloc(row * sizeof(int *));
    for (int i = 0; i < row; ++i){
        data[i] = (double *)malloc(col * sizeof(double));
    }//动态申请二维数组
    get_two_dimension(line, data, filename);


    AngleItem angle;

    angle.openItem.ave = getAverage(data,1,col);
    angle.closeItem.ave = getAverage(data,2,col);
    angle.openItem.max = getMax(data,1,col);
    angle.closeItem.max = getMax(data,2,col);

    return angle;

}



